﻿using System;
using System.Xml.Serialization;
using PrearrangedChaos.AgileZen.Service.Rest;

namespace PrearrangedChaos.AgileZen.Service.Model
{
    [XmlType(AnonymousType = true)]
    [XmlRootAttribute(ElementName = "task", Namespace = "", IsNullable = false)]
    [ZenRestRequest("projects/{ProjectId}/stories/{StoryId}/tasks/{TaskId}")]
    public class TaskUpdater : ModelBase, ITaskUpdater
    {
        private string _text;
        private TaskStatus? _status;

        /// <remarks/>
        [XmlElement(ElementName = "text")]
        public string TextXmlElement
        {
            get { return _text; }
            set
            {
                if (!string.IsNullOrEmpty(value))
                    _text = value;
                else
                    _text = null;

                this.Unmodified("text");
            }
        }

        [XmlIgnore]
        public string Text
        {
            get { return _text; }
            set
            {
                if (_text == value)
                    return;

                _text = value;
                this.Modified("text");
            }
        }

        public bool TextSpecified { get { return this.IsModified("text"); } }

        [XmlElement(ElementName = "status")]
        public string StatusXmlElement
        {
            get { return this.Status == null ? null : this.Status.Value.ToString().ToLower(); }
            set
            {
                this.Status = (TaskStatus)Enum.Parse(typeof(TaskStatus), value, true);
                this.Unmodified("status");
            }
        }

        public bool StatusXmlElementSpecified { get { return this.IsModified("status"); } }

        [XmlIgnore]
        public TaskStatus? Status
        {
            get { return _status; }
            set
            {
                if (_status == value)
                    return;

                _status = value;
                this.Modified("status");
            }
        }
    }
}